package connection;

import messages.Message;
import messages.StartMessage;
import messages.WelcomeMessage;
import guicomponents.BattleSquare;
import guicomponents.Gui;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Connection extends Thread {

    private static Connection INSTANCE;
    private static Socket socket;
    private static int port;
    private static String ip;
    private static boolean CONNECTED = false;

    private Connection() {
    }

    public static Connection getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Connection();
        }
        return INSTANCE;
    }

    public boolean setConnection(String ip, int port) {
        try {
            socket = new Socket(ip, port);
            CONNECTED = true;
            System.out.println(socket.isConnected());
        } catch (UnknownHostException ex) {
            CONNECTED = false;
            System.out.println("Nem sikerült kapcsolódni");
        } catch (IOException ex) {
            CONNECTED = false;
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return CONNECTED;
    }

    public Message getMessage() {
        ObjectInputStream ios = null;
        try {
            ios = new ObjectInputStream(this.socket.getInputStream());
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Message message = null;
        try {
            message = (Message) ios.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return message;
    }

    public void sendMess(Message mess) throws IOException {
        OutputStream os = this.socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(mess);
        oos.flush();
    }

    @Override
    public void run() {
            //Ha megjön a welcome akkor lerakni a hajókat
            //if("WelcomeMessage".equals(getMessage().getType())) {
            if(getMessage().getType().equals("WelcomeMessage")) {
                Gui.buttonSetter(true);
                Gui.status.setText("Set your ships to the field!");
                if(BattleSquare.testShips()) {
                try {
                    sendMess(new StartMessage("Let the War begin!"));
                } catch (IOException ex) {
                    Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                  }
                }
            }          
            //elkezdődik a csata Player1 kezd Player2 vár
            if("WaitMessage".equals(getMessage().getType())) {
                Gui.status.setText("Waiting for player...");
                //kiértékelni a hittmesst
            }
            if("MoveMessage".equals(getMessage().getType())) {
                Gui.status.setText("Fire!");
            }
     }
}

